package cn.lyjuan.android.mylibrary.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;

import cn.lyjuan.base.util.Base64;

/**
 * Created by chad on 08/03/2018.
 */

public class Base64AndroidUtils
{
    /**
     * 将字符串转换成Bitmap类型
     * @param string
     * @return
     */
    public static Bitmap toBitmap(String string)
    {
        Bitmap bitmap = null;
        try
        {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        return bitmap;
    }

    /**
     * 将Bitmap转换成字符串
     *
     * @param bitmap
     * @return
     */
    public static String fromBitmap(Bitmap bitmap)
    {
        String string = null;
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
        byte[] bytes = bStream.toByteArray();
        string = Base64.encode(bytes);
        return string;
    }
}
